//
//  NSArray+Category.m
//  YLSight
//
//  Created by Zoo on 2017/5/4.
//  Copyright © 2017年 Zoo. All rights reserved.
//

#import "NSArray+Category.h"

@implementation NSArray(Category)

+ (NSArray*)clearSameObject:(NSArray *)array {
    NSMutableArray * maTmp = [NSMutableArray arrayWithArray:array];
    NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [maTmp enumerateObjectsUsingBlock:^(id  _Nonnull other, NSUInteger idx1, BOOL * _Nonnull stop) {
            if ([obj isEqual:other]) [indexSet addIndex:idx1];
        }];
        [indexSet removeIndex:[indexSet lastIndex]];
        if ([indexSet count]) {
            [maTmp removeObjectsAtIndexes:indexSet];
            [indexSet removeAllIndexes];
        }
    }];
    return [maTmp copy];
}

@end

@implementation NSMutableArray (Category)

- (void)clearSameObject {
    NSArray * aTmp = [self copy];
    NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
    [aTmp enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop) {
        [self enumerateObjectsUsingBlock:^(id  _Nonnull other, NSUInteger idx2, BOOL * _Nonnull stop) {
            if ([other isKindOfClass:[NSDictionary class]]) {
                if ([(NSDictionary*)obj isEqualToDictionary:(NSDictionary*)other]) [indexSet addIndex:idx2];
            } else {
                if ([obj isEqual:other]) [indexSet addIndex:idx2];
            }
        }];
        [indexSet removeIndex:[indexSet lastIndex]];
        if ([indexSet count]) {
            [self removeObjectsAtIndexes:indexSet];
            [indexSet removeAllIndexes];
        }
    }];
}

@end
